package com.punch.punchtable.controller;

import com.punch.punchtable.entity.Sumup;
import com.punch.punchtable.service.SumupService;
import com.punch.punchtable.vo.DepartmentVO;
import com.punch.punchtable.vo.PageResponseVO;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private SumupService sumupService;
    @GetMapping("/page")
    @ResponseBody
    public PageResponseVO page(String dept, Integer page, Integer limit,
                               @RequestParam(name = "date",defaultValue = "",required = false) String date){

        String year = "";
        String month = "";
        String sql = "where 1=1";
        if (!StringUtils.isEmpty(date)){
            String[] split = date.split("-");
            year = split[0];
            month = split[1];
            sql = sql+" and year = "+year+" and month = "+month;
        }
        if (!StringUtils.isEmpty(dept)){
            sql = sql+" and dept = '"+dept + "'";
        }
        Pageable pageable = new PageRequest(page-1,limit);
        List<DepartmentVO> departmentVOS = sumupService.departmentList(pageable, sql);
        Long count = sumupService.departmentListCount(sql);

        return PageResponseVO.response(departmentVOS,count);
    }
}
